package com.jlastu.dlxy_androidapp.adapter;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.entity.School;
import com.jlastu.dlxy_androidapp.entity.SchoolPhoto;
import com.jlastu.dlxy_androidapp.view.main.SchoolDetailsActivity;

import java.util.List;

public class NewsListAdapter extends RecyclerView.Adapter<NewsListAdapter.ViewHolder> {

    private List<School> mList;

    private List<SchoolPhoto> mList_P;

    public NewsListAdapter(List<School> list, List<SchoolPhoto> list_P){
        mList = list;
        mList_P = list_P;

    }
    @SuppressLint("NotifyDataSetChanged")
    public void updateDate(List<School> list, List<SchoolPhoto> list_P){
        mList.clear();
        mList = list;
        mList_P = list_P;
        notifyDataSetChanged();

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull NewsListAdapter.ViewHolder holder, int position) {
        School school = mList.get(position);
        holder.textView1.setText(school.getName());
        holder.textView2.setText(school.getClassify());
        if ( position >= mList.size() || position >= mList_P.size()) {
            // 显示错误图标
            holder.photo.setImageResource(R.drawable.attention);
            return;
        }
        SchoolPhoto photo = mList_P.get(position);
        holder.photo.setImageBitmap(photo.getBitmap());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView photo;
        TextView textView1;
        TextView textView2;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 获取点击的列表项位置
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        // 处理点击事件，例如启动新的Activity
                        School school = mList.get(position);
                        Intent intent = new Intent(itemView.getContext(), SchoolDetailsActivity.class);
                        intent.putExtra("school_id", school.getId());
                        itemView.getContext().startActivity(intent);

                    }
                }
            });
            photo = itemView.findViewById(R.id.news_photo);
            textView1 = itemView.findViewById(R.id.news_name);
            textView2 = itemView.findViewById(R.id.news_classify);
        }
    }
}
